import { defineStore } from 'pinia'
import type { NoticeState } from '@/stores/modules/types/noticeState'
import { store } from '@/stores'
import { message } from 'ant-design-vue'
import { getByUserMessageAPI, readNoticeAPI } from '@/views/admin/system/notice/utils/api'

export const useNoticeStore = defineStore({
  id: 'simple-notice',
  state: (): NoticeState => ({
    list: [],
    unreadInformation: 0
  }),
  getters: {},
  actions: {
    /**
     * 获取用户未读消息
     */
    getByUserMessage() {
      getByUserMessageAPI(false).then((res) => {
        this.list = res.data
        this.unreadInformation = res.data.length
      })
    },
    /**
     * 已读消息
     */
    readMessage() {
      const ids = this.list.map((item) => item.id)
      readNoticeAPI(ids).then(() => {
        this.unreadInformation = 0
        message.success('操作成功')
      })
    }
  }
})

export const useNoticeStoreHook = () => {
  return useNoticeStore(store)
}
